﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GetTogether.Studio.WebService;

public partial class WebService_Edit : GetTogether.Studio.Web.UI.Page
{
    public ProjectParameter Parameter;
    public string ProjectName
    {
        get
        {
            return Request["pn"];
        }
    }

    public string Codes = string.Empty;
    private string GetPath(string username)
    {
        string methodName = Request["method"];
        string historyName = Request["history"];
        string sourcePath = System.IO.Path.Combine(string.Concat(GetTogether.Studio.WebService.ProjectParameter.GetSettingsPath(username), Parameter.ProjectName, "(History)"), methodName);
        sourcePath = System.IO.Path.Combine(sourcePath, historyName);
        return sourcePath;
    }
    protected string GetProxyAddress()
    {
        return string.Format(string.Concat("http://{0}/{1}/{2}/{3}", WebServiceProxy.Suffix), string.Concat(Request.Url.Host, Request.Url.Port == 80 ? "" : (":" + Request.Url.Port.ToString())), Request.Url.AbsolutePath.Split('/')[1], CurrentSession.UserCode, ProjectName);
    }
    private string GetInvokeHistoryPath(string methodName)
    {
        string path = GetTogether.Studio.WebService.ProjectParameter.GetSettingsPath(CurrentSession.UserCode);
        path = System.IO.Path.Combine(path, string.Concat(ProjectName, "(History)\\", methodName));
        string htyName = Request["history"];
        if (string.IsNullOrEmpty(htyName)) htyName = "Recent";
        path = System.IO.Path.Combine(path, htyName);
        return path;
    }
    private string GetInvokeHistoryParameterFile(string methodName, string pName)
    {
        return System.IO.Path.Combine(GetInvokeHistoryPath(methodName), string.Concat(pName, ".txt"));
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Parameter = ProjectParameter.GetSettingsByProjectName(CurrentSession.UserCode, ProjectName, CurrentSession.ShareUserCode);

        #region From Logging
        string loggingSoapRequest = Request.Form["result-Soap"];
        string loggingAddress = Request.Form["Address"];
        string methodName = Request["method"];

        if (!string.IsNullOrEmpty(loggingSoapRequest))
        {
            if (Parameter == null)
            {
                ProjectParameter st = new ProjectParameter();
                st.ProjectName = ProjectName;
                st.Address = loggingAddress;
                st.AddressType = AddressType.WebService;
                string filePath = string.Concat(ProjectParameter.GetSettingsPath(CurrentSession.UserCode), st.ProjectName);
                if (System.IO.File.Exists(filePath))
                {
                    Response.Clear();
                    Response.Write(string.Concat("The project \"", st.ProjectName, "\" already existed."));
                    Response.End();
                }
                else
                {
                    System.IO.File.WriteAllText(ProjectParameter.GetSettingsPath(CurrentSession.UserCode) + st.ProjectName, st.ToXml());
                }
                Parameter = ProjectParameter.GetSettingsByProjectName(CurrentSession.UserCode, ProjectName, CurrentSession.ShareUserCode);
            }
            MethodSetting methodSetting = new MethodSetting();
            methodSetting.RqtMode = MethodSetting.RequestMode.SOAP;
            string historyPath = GetInvokeHistoryPath(methodName);
            if (!System.IO.Directory.Exists(historyPath)) System.IO.Directory.CreateDirectory(historyPath);
            System.IO.File.WriteAllText(GetInvokeHistoryParameterFile(methodName, "[Request-Mode]"),
                GetTogether.Utility.SerializationHelper.SerializeToXml(methodSetting));
            System.IO.File.WriteAllText(GetInvokeHistoryParameterFile(methodName, "[SOAP-Request]"), loggingSoapRequest);
        }
        #endregion

        this.Title = ProjectName;
        if (Parameter == null)
        {
            Response.Redirect("~/WebService");
        }
        if (!string.IsNullOrEmpty(CurrentSession.ShareUserCode))
        {
            GetTogether.Utility.DirectoryHelper.CopyParameter cp = new GetTogether.Utility.DirectoryHelper.CopyParameter();
            cp.Source = GetPath(CurrentSession.ShareUserCode);
            if (System.IO.Directory.Exists(cp.Source))
            {
                cp.Destination = GetPath(CurrentSession.UserCode);
                if (!System.IO.Directory.Exists(cp.Destination)) System.IO.Directory.CreateDirectory(cp.Destination);
                cp.IsOverwrite = true;
                GetTogether.Utility.DirectoryHelper.Copy(cp);
            }
        }
        if (!string.IsNullOrEmpty(Request["gc"]))
        {
            string wsdlAddress = Parameter.WsdlAddress;
            if (string.IsNullOrEmpty(wsdlAddress)) wsdlAddress = string.Concat(Parameter.Address, "?WSDL");
            Codes = WsdlHelper.GetCodes(wsdlAddress);
            Response.ContentType = "text/plain";
            Response.Write(Codes);
            Response.End();
        }
        else if (Request["share-type"] == "1" || !string.IsNullOrEmpty(Request["from-schedule"]))
        {
            string ret = GetTogether.Studio.WebService.Util.InvokeByShare(CurrentSession.UserCode, methodName, ProjectName, CurrentSession.ShareUserCode, Request["history"]);
            Response.Write(ret);
            Response.ContentType = "text/plain";
            Response.End();
        }
        else
        {
            GetTogether.Studio.ProjectHistory.SaveHistory(GetTogether.Studio.ProjectParameter.ProjectCategory.WebService, ProjectName, CurrentSession.UserCode);
        }
    }
}